import {
    $get,
    $post,
    $POST,
    BASE_URL
} from '@/puigins/request.js';

export function mkdir(workDirId, newDirName) {
    return new Promise((resolve, reject) => {
        $post('/netpan/mkdir', { workDirId, newDirName }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function getFiles(dir) {
    return new Promise((resolve, reject) => {
        $get('/netpan/getFiles', { dir }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}

export function rm(id) {
    return new Promise((resolve, reject) => {
        $post('/netpan/rm', { id }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function rename(id, newName) {
    return new Promise((resolve, reject) => {
        $post('/netpan/rename', { id, newName }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}

export function uploadFragment(token, index, file, onUploadProgress) {
    console.log(file)
    return new Promise((resolve, reject) => {
        $POST('/netpan/fragment', { token, index, file }, { onUploadProgress, responseType: 'json' }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function upload(token) {
    return new Promise((resolve, reject) => {
        $post('/netpan/upload', { token }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function prepareUpload(size, fileName, workDirId, dir) {
    return new Promise((resolve, reject) => {
        $post('/netpan/prepareUpload', { size, fileName, workDirId, dir }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function getDownloadToken(workDirId,selectedFileIds) {
    return new Promise((resolve, reject) => {
        $get('/netpan/getDownloadToken', { workDirId,selectedFileIds }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function deleteFile(ids) {
    return new Promise((resolve, reject) => {
        $post('/netpan/deleteFile', { ids }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function cp(ids, workDirId) {
    return new Promise((resolve, reject) => {
        $post('/netpan/cp', { ids, workDirId }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function cut(ids, workDirId) {
    return new Promise((resolve, reject) => {
        $post('/netpan/cut', { ids, workDirId }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export function search(name) {
    return new Promise((resolve, reject) => {
        $get('/netpan/search', { name }).then(({ data }) => {
            resolve(data)
        }).catch(reject)
    })
}
export const PREVIEW_IMAGE_URL = BASE_URL + 'netpan/previewImage?id='
export const TEMP_DOWNLOAD_URL = BASE_URL + 'netpan/tempDownload'